
package com.hbyj.struts.action.authority;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.DepartmentCategory;
import com.hbyj.hbm.vo.Departments;
import com.hbyj.object.DepartmentForFlexGrid;
import com.hbyj.object.DepartmentUpdateObject;
import com.hbyj.object.FlexGridData;
import com.hbyj.object.RoleAddData;


import com.opensymphony.xwork2.ActionSupport;

/**
 * @author cAn
 * @since JDK 1.5
 * @version Jun 4, 2010 11:41:58 AM
 */
@SuppressWarnings("serial")
public class AuthorityDepartmentAction extends ActionSupport {
	private String jsonResult;
	
	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	
	public void department_add() throws UnsupportedEncodingException, IOException
	{
//		HttpServletRequest request = ServletActionContext.getRequest();
//		String name = request.getParameter("name");
//		String category = request.getParameter("category");
//		
//		
//		List departCategoryList = DbTools.findByProperty("DepartmentCategory", "name", category);
//		Departments department = new Departments((DepartmentCategory)departCategoryList.get(0),name);
//		Boolean rlt=DbTools.saveObj(department);
//		ServletActionContext.getResponse().getOutputStream().write((rlt.toString()).getBytes("utf-8"));
	}
	
	public void department_add_regioncode() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String name = request.getParameter("name");
		String regionCode = request.getParameter("code");
		
		Departments department = new Departments(null,name,regionCode);
		Boolean rlt = DbTools.saveObj(department);
		ServletActionContext.getResponse().getOutputStream().write((rlt.toString()).getBytes("utf-8"));
	}
	
	public void department_delete() throws UnsupportedEncodingException, IOException{
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer department_id = Integer.parseInt(request.getParameter("department_id"));
			Boolean rltBoolean=DbTools.delete("delete from Departments department where department.id="+department_id);
			ServletActionContext.getResponse().getOutputStream().write((rltBoolean.toString()).getBytes("utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
			ServletActionContext.getResponse().getOutputStream().write((e.toString()).getBytes("utf-8"));
		}
	}
	
  public void department_query() throws UnsupportedEncodingException,IOException
  {
	  HttpServletRequest request = ServletActionContext.getRequest();
	  
	  Integer page =Integer.parseInt(request.getParameter("page"));
	  Integer rp = Integer.parseInt(request.getParameter("rp"));
	  
	  Integer start =(page-1)*rp;
	  Integer end =page * rp;
	  
 	  List departmentsTotal = DbTools.findByHql("from Departments");
	  List departments = DbTools.findPageHQL(start, end, "from Departments");

	  int total =departmentsTotal.size();
	  
	  FlexGridData data = new FlexGridData(total, page, departments);
	  String  jsonText= JSONObject.fromObject(data).toString();
	  ServletActionContext.getResponse().getOutputStream().write(jsonText.getBytes("utf-8"));
  }
  
  public void department_update() throws UnsupportedEncodingException, IOException{
	  try {
		  HttpServletRequest request = ServletActionContext.getRequest();
		  String departmentString = request.getParameter("data");
		   JSONObject jsonObj = JSONObject.fromObject(departmentString);
		   DepartmentForFlexGrid departmentPost = (DepartmentForFlexGrid)JSONObject.toBean(jsonObj,DepartmentForFlexGrid.class);
		   Boolean result=DbTools.saveOrUpdateObj(departmentPost.GetHiberObject());
		   ServletActionContext.getResponse().getOutputStream().write((result.toString()).getBytes("utf-8"));
	} catch (Exception e) {
		e.printStackTrace();
		ServletActionContext.getResponse().getOutputStream().write((e.toString()).getBytes("utf-8"));
	}
	
  }
}













